<?php

/**
 * Gets content objects from the database.
 */
m2import('phpinchains/psp/Tag.php');

class cms_getlocationobjectsHandler extends phpinchains_psp_Tag
{

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_BODY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName   = 'cms:getlocationobjects';
    var $requiredAttributes = array(
        'var' => '[\w\d_]+',
    );
    var $attributes = array(
        'var',
        'varNumResults',
        'name', // location name
        'lang',  // lang of content objects
        'id'     // document id (optional) if not set, current document id is used
    );
    
    function doStartTag()
    {
        // initializes $this->buffer and pushes the listener on the stack of the 
        // main output writer
        return PSP_EVAL_BODY_BUFFERED;
    }

    function doEndTag()
    {
        $lang = $this->getAttribute('lang');
        $ctx = base_ApplicationContext::getInstance();
        if (!$lang)
            $lang = ($ctx->getDocumentContext()->getLanguage('id'));
        $dba = $ctx->getAccessFactory()->getInstanceOf(base_AccessFactory::$CMS);
        
        $docid = (int)$this->getAttribute('id')?$this->getAttribute('id'):$ctx->getDocumentContext()->getDocId();
        
        if ($ctx->getDocumentContext()->isLive())
            $res = $dba->getPubLocationObjects($docid, $this->getAttribute('name'), $lang);
        else
            $res = $dba->getLocationObjects($docid, $this->getAttribute('name'), $lang);
        if (is_array($res))
        foreach ($res as $n => $v) {
            $v['data']['meta'] = $v;
            $v = $v['data'];
            $res[$n] = $v;
        }
        $this->actionform->set($this->getAttribute('var'), $res);
        if ($this->getAttribute('varNumResults'))
            $this->actionform->set($this->getAttribute('varNumResults'), $res['count']);
        return PSP_SKIP_BODY;
    }

}

